/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: avp_container.c
 *
 * Purpose: implementation container for av player.
 *
 * Developer:
 *   wen.gu , 2022-01-18
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "avp_container.h"
#include "avp_mutex.h"
#include "avp_log.h"

#ifdef AVP_CONTAINER_FFMPEG
#include "ff_container.h"
#endif

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/
typedef struct _avp_container_node
{
    struct _avp_container_node* next;
    const avp_container_ops *container;
}avp_container_node_t;

static GU32 g_is_container_inited = 0;
static avp_mutex_t g_container_lock = NULL;
static avp_container_node_t* g_container_list_head = NULL;

typedef void(*avp_container_ctor)(void);

static avp_container_ctor g_container_ctor[] = 
{
#ifdef AVP_CONTAINER_FFMPEG
    ff_container_register,
#endif 
    NULL
};

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

static avp_container_node_t* avp_container_node_new(const avp_container_ops* container)
{
    avp_container_node_t* node = (avp_container_node_t*)malloc(sizeof(avp_container_node_t));

    if (node)
    {
        node->next = NULL;
        node->container = container; /** todo refine me?? */ 
        return node;       
    }   
    
    LOGE("malloc container list node failed\n");

    return NULL;
}

static avp_container_node_t** avp_container_node_tail()
{
    avp_container_node_t* node = g_container_list_head;

    while (node)
    {
        if (node->next == NULL)
        {
            return &node->next;
        }

        node = node->next;
    }

    return &g_container_list_head;
}
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

GErrc avp_container_initialize()
{
    if (!g_is_container_inited && !g_container_lock)
    {
        g_container_lock = avp_mutex_create();
        g_container_list_head = NULL;
        g_is_container_inited = 1;
        return G_OK;
    }

    return G_ErrInvalidStatus;
}

void avp_container_finish()
{
    if (g_is_container_inited && g_container_lock)
    {
        avp_mutex_lock(g_container_lock);
        avp_container_node_t* item = g_container_list_head;
        while (item)
        {
            avp_container_node_t* next = item->next;
            free(item);
            item = next;
        }
        avp_mutex_t temp_lock = g_container_lock;
        g_is_container_inited = 0;
        g_container_lock = NULL;
        g_container_list_head = NULL;
        avp_mutex_unlock(temp_lock);
        avp_mutex_destroy(temp_lock);
    }
}

GErrc avp_container_register(const avp_container_ops* container_ops)
{
    if (!container_ops)
    {
        return G_ErrBadParameter;
    }

    if (g_is_container_inited && g_container_lock)
    {
        avp_mutex_lock(g_container_lock);

        avp_container_node_t* node = avp_container_node_new(container_ops);

        if (!node)
        {
            LOGE("malloc container list node failed\n");
            avp_mutex_unlock(g_container_lock);
            return G_ErrInsufficientResources;
        }    

        avp_container_node_t** tail = avp_container_node_tail();
        *tail = node;

        avp_mutex_unlock(g_container_lock);
        return G_OK;
    }

    return G_ErrInvalidStatus;
}

const GErrc avp_container_find(const GSTR url, avp_container_t* container)
{
    if (!url || !container)
    {
        return G_ErrBadParameter;
    }

    if (g_is_container_inited && g_container_lock)
    {
        const avp_container_ops* container_ops = NULL;
        avp_mutex_lock(g_container_lock);
        avp_container_node_t* item = g_container_list_head;
        while (item)
        {
            if (item->container->initialize)
            {
                if (item->container->initialize(&container->handle, url) == G_OK)
                {
                    container->ops = item->container;
                    avp_mutex_unlock(g_container_lock);
                    return G_OK;
                }
            }
            item = item->next;
        }

        avp_mutex_unlock(g_container_lock);
        return G_ErrNotFound;
    }

    return G_ErrInvalidStatus;;
}

void avp_container_ops_finish(const avp_container_t* container)
{
    if (container->ops && container->ops->finish)
    {
        container->ops->finish(container->handle);
    }
}


GErrc avp_container_ops_stream_get_info(const avp_container_t* container, GS32 index, avp_media_stream_t* stream_info)
{
    if (container->ops && container->ops->stream_get_info)
    {
        return container->ops->stream_get_info(container->handle, index, stream_info);
    }

    return G_ErrNotImplemented;
}

GErrc avp_container_ops_stream_select(const avp_container_t* container, GU32 index)
{
    if (container->ops && container->ops->stream_select)
    {
        return container->ops->stream_select(container->handle, index);
    }

    return G_ErrNotImplemented;
}

GErrc avp_container_ops_stream_unselect(const avp_container_t* container, GU32 index)
{
    if (container->ops && container->ops->stream_unselect)
    {
        return container->ops->stream_unselect(container->handle, index);
    }

    return G_ErrNotImplemented;    
}

avp_media_category_t avp_container_ops_stream_category(const avp_container_t* container, GU32 index)
{
    if (container->ops && container->ops->stream_category)
    {
        return container->ops->stream_category(container->handle, index);
    }

    return AVP_MEDIA_CATEGORY_UNKNOWN;    
}


GErrc avp_container_ops_read(const avp_container_t* container, avp_packet_t** pkt)
{
    if (container->ops && container->ops->read)
    {
        return container->ops->read(container->handle, pkt);
    }

    return G_ErrNotImplemented;
}

GU32 avp_container_ops_stream_count(const avp_container_t* container)
{
    if (container->ops && container->ops->stream_count)
    {
        return container->ops->stream_count(container->handle);
    }

    return 0;
}

GU32 avp_container_ops_stream_count_ex(const avp_container_t* container, avp_media_category_t category)
{
    if (container->ops && container->ops->stream_count_ex)
    {
        return container->ops->stream_count_ex(container->handle, category);
    }

    return 0;
}
